package com.bugpool.leilema.tc.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.bugpool.leilema.freamwork.satoken.SaTokenUtil;
import com.bugpool.leilema.tc.bean.entity.BoundaryEntity;
import com.bugpool.leilema.tc.bean.entity.SysRoleDataTree;
import com.bugpool.leilema.tc.bean.entity.SysUser;
import com.bugpool.leilema.tc.bean.entity.TreeEntity;
import com.bugpool.leilema.tc.bean.vo.SysUserVo;
import com.bugpool.leilema.tc.mapper.SysRoleDataTreeMapper;
import com.bugpool.leilema.tc.mapper.TreeMapper;
import com.bugpool.leilema.tc.service.TreeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.parsers.SAXParser;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wh
 * @Description
 * @Date 2023/2/28 9:12
 * @Version 1.0
 **/
@Service
public class TreeServiceImpl implements TreeService {

    @Resource
    private TreeMapper treeMapper;

    @Resource
    private SysRoleDataTreeMapper roleDataTreeMapper;


    @Override
    public List<Tree<Long>> listTree() {
        //判断当前用户是否是超级管理员 (超级管理员可以看全部的树)
        SysUserVo user = SaTokenUtil.getUser();
        List<TreeEntity> treeEntityList=new ArrayList<>();
         treeEntityList=user.isAdmin()?
                 treeMapper.selectList(null):treeMapper.selectByUserId(user.getUserId());
        return buildTree(treeEntityList);

    }

    @Override
    public boolean addTree(TreeEntity tree) {
        //添加
        treeMapper.insert(tree);
        return true;
    }

    @Override
    public boolean updateTree(TreeEntity tree) {
        //根据id 修改
        treeMapper.updateById(tree);
        return true;
    }

    @Override
    public TreeEntity getTreeEntity(Long id) {
        //根据id 获取详情
        return treeMapper.selectById(id);
    }

    @Override
    public boolean delByIds(List<Long> ids) {
        //根据id集合删除
        treeMapper.deleteBatchIds(ids);
        return true;

    }

    @Override
    public List<Tree<Long>> listTreeByUserId(Long userId) {
        List<TreeEntity> treeEntityList = treeMapper.selectByUserId(userId);
        //构建树                 数据                顶级父id
        return buildTree(treeEntityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean buildRoleTree(List<Long> treeIds,Long roleId) {
      //删除关联的角色数据
        roleDataTreeMapper.deleteByRoleId(roleId);
        //重新绑定关系
        SysRoleDataTree roleDataTree = new SysRoleDataTree();
        treeIds.stream().forEach(treeId->{
            roleDataTree.setRoleId(roleId);
            roleDataTree.setTreeId(treeId);
            roleDataTreeMapper.insert();
        });
        return false;
    }

    private List<Tree<Long>> buildTree(List<TreeEntity> treeEntityList) {
        return TreeUtil.build(treeEntityList, 0L,
                new TreeNodeConfig().setDeep(10),      //遍历深度
                (tree, treeNode) -> {
                    //构建树节点
                    treeNode.setId(tree.getId());
                    treeNode.setParentId(tree.getParentId());
                    //拓展
                    treeNode.putExtra("label", tree.getLabel());
                    treeNode.putExtra("uri", tree.getUri());
                    treeNode.putExtra("type", tree.getType());
                    treeNode.putExtra("boundary", BoundaryEntity.builder()
                            .W(tree.getW())
                            .S(tree.getS())
                            .E(tree.getE())
                            .N(tree.getN()).build());
                }
        );
    }
}
